package csl.blog.client.controller;

import cn.hutool.json.JSONUtil;
import csl.blog.client.netty.NioNettyClient;
import csl.blog.client.service.ConsumerService;
import csl.blog.common.base.netty.MessageBo;
import csl.blog.common.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Description: TODO
 * @author: 隔山海
 * @date: 2021年07月15日 13:46
 */
@Slf4j
@RequestMapping("/test")
@RestController
public class TestController {

    /**
     * OpenFeign调用
     */
    @Autowired
    private ConsumerService consumerServer;

    /**
     * Ribbo调用
     */
    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    private static final String SERVER_NAME = "http://blog-server-dev";

    @Autowired
    private NioNettyClient nioNettyClient;

    @GetMapping("/queryByCondition/{userId}")
    public List<User> list(@PathVariable String userId) {
        return consumerServer.queryByCondition(userId);
    }

    @PostMapping("/add")
    public int add(@RequestBody User user) {
        return consumerServer.add(user);
    }

    @PostMapping("/send")
    public void send(@RequestBody MessageBo message) {
      log.info(JSONUtil.toJsonStr(JSONUtil.toJsonStr(message)));
      nioNettyClient.sendMsg(JSONUtil.toJsonStr(message));
    }

    @GetMapping("/token")
    public String token() {
        return restTemplate.getForObject(SERVER_NAME + "/test/token", String.class);
    }
}
